package components.pdf;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
import components.JDErrorDialog;
import components.JDVerticalFlowLayout;

public class PanelReport extends JPanel {
	private static final long serialVersionUID = 1L;

	private JTextArea textArea = new JTextArea();
	// private JScrollPane scrollPane = new JScrollPane(textArea);
	private JPanel panelCenter = new JPanel(new JDVerticalFlowLayout(JDVerticalFlowLayout.CENTER, 20, 20, true, true));
	private JPanel panelPreview = new JPanel(new BorderLayout());

	private JPanel panelEast = new JPanel(new JDVerticalFlowLayout(JDVerticalFlowLayout.TOP, 2, 2, false, true));

	private File file;

	private int margin = 20;

	private JButton button = new JButton("Save");
	private JButton buttonOpen = new JButton("Open in Pdf Viewer");

	public PanelReport(File file) {
		this.file = file;

		setPreferredSize(new Dimension(400, 600));

		panelPreview.add(textArea);

		textArea.setMargin(new Insets(margin, margin, margin, margin));

		panelCenter.add(panelPreview);

		panelPreview.setBackground(Color.white);

		panelPreview.setBorder(BorderFactory.createLineBorder(Color.BLACK));

		setLayout(new BorderLayout());
		add(panelCenter);
		add(panelEast, BorderLayout.EAST);

		panelEast.add(button);

		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				createPdf();
			}
		});
		
		buttonOpen.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				open();
			}
		});
		
	}

	protected void open() {
		
		createPdf();
		
		Desktop desktop = Desktop.getDesktop();

		try {
			desktop.open(file);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

	}

	public void createPdf() {

		boolean shapes = true;
		
		Document document = new Document();
		try {
			getRootPane().getParent().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

			PdfWriter writer;
			if (shapes)
				writer = PdfWriter.getInstance(document, new FileOutputStream(file));
			else
				writer = PdfWriter.getInstance(document, new FileOutputStream(file));
			document.open();
			PdfContentByte cb = writer.getDirectContent();
			PdfTemplate tp = cb.createTemplate(500, 500);
			Graphics2D g2;
			if (shapes)
				g2 = tp.createGraphicsShapes(500, 500);
			else
				g2 = tp.createGraphics(500, 500);

			textArea.print(g2);

			g2.dispose();
			cb.addTemplate(tp, 30, 300);
		} catch (Exception e) {
			JDErrorDialog.reportException(e);
		} finally {
			getRootPane().getParent().setCursor(null);
		}
		document.close();
	}

	private static void createAndShowGUI() {
		// Create and set up the window.
		JFrame frame = new JFrame("ProgressBarDemo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Create and set up the content pane.
		JComponent newContentPane = new PanelReport(new File("c:/temp/report.pdf"));
		newContentPane.setOpaque(true); // content panes must be opaque
		frame.setContentPane(newContentPane);

		// Display the window.
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		// Schedule a job for the event-dispatching thread:
		// creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				createAndShowGUI();
			}
		});
	}

	// Graphics2D g2 = cb.createGraphics(612, 792, mapper, true, .95f);
	// AffineTransform at = new AffineTransform();
	// at.translate(convertToPixels(20), convertToPixels(20));
	// at.scale(pixelToPoint, pixelToPoint);
	// g2.transform(at);
	// g2.setColor(Color.WHITE);
	// g2.fill(ta.getBounds());
	// Rectangle alloc = getVisibleEditorRect(ta);
	// ta.getUI().getRootView(ta).paint(g2, alloc);
	// g2.setColor(Color.BLACK);
	// g2.draw(ta.getBounds());
	// g2.dispose();

}
